var express = require("express");
var router = express.Router();
const moviesApi = require("../api/movies")

// lzy
router.get('/all', async function (req, res, next) {
    const data = await moviesApi.getAll();
    res.send(data);
});

//电影信息列表
router.get("/movies", async function (req, res, next) {
    const { page, limit } = req.query;
    const data = await moviesApi.get({ page, limit });
    res.send({ "code": 0, "msg": "", "count": data.total, "data": data.data, });
});
//获取某一条电影信息
router.get('/:_id', async function (req, res, next) {
    const { _id } = req.params;
    const data = await moviesApi.getOne({ _id });
    res.send(data)
});
//修改电影信息
router.put("/:_id", async function (req, res, next) {
    const { _id } = req.params;
    const movies = req.body;
    const data = await moviesApi.update({ _id, movies });
    res.send(data);
});
//删除某一条的电影信息
router.delete("/:_id", async function (req, res, next) {
    console.log(req.params);
    const { _id } = req.params;
    console.log(_id);
    const data = await moviesApi.removeMovie({ _id });
    res.send(data)
})
//添加电影
router.post("/add", async function (req, res, next) {
    const data = await moviesApi.add(req.body);
    res.send(data);
})
//给某一条电影添加图片信息
router.patch("/:_id/images/:url", async function (req, res, next) {
    const { _id, url } = req.params;
    const data = await moviesApi.addImg({ _id, url });
    res.send(data);
});
//给电影添加海报
router.patch("/:_id/poster/:url", async function (req, res, next) {
    const { _id, url } = req.params;
    const data = await moviesApi.addPoster({ _id, url });
    res.send(data);
});

//删除某一条电影的图片信息
router.delete("/:_id/images/:url", async function (req, res, next) {
    const { _id, url } = req.params;
    const data = await moviesApi.removeImg({ _id, url });
    res.send(data);
})
module.exports = router;